package com.falcon.service.impl;

import java.util.List;

import com.falcon.domain.dos.QuestionCategoryDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.QuestionCategoryPO;
import com.falcon.domain.req.QuestionCategoryQuery;
import com.falcon.domain.req.QuestionCategoryRequest;

import com.falcon.mapper.IQuestionCategoryMapper;
import com.falcon.service.IQuestionCategoryService;


@Slf4j
@Service
public class QuestionCategoryService implements IQuestionCategoryService {

    @Autowired
    private IQuestionCategoryMapper questionCategoryMapper;

    @Override
    public Long save(QuestionCategoryRequest request){
        QuestionCategoryPO questionCategoryPO = BeanUtil.copyProperties(request, QuestionCategoryPO.class);

        this.questionCategoryMapper.insertNotNull(questionCategoryPO);

        return questionCategoryPO.getId();
    }

    @Override
    public Boolean update(Long id, QuestionCategoryRequest request){

        QuestionCategoryPO questionCategoryPO = BeanUtil.copyProperties(request, QuestionCategoryPO.class);
        questionCategoryPO.setId(id);

        return this.questionCategoryMapper.updateNotNull(questionCategoryPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.questionCategoryMapper.deleteById(id);
    }

    @Override
    public QuestionCategoryDO queryById(Long id){

        QuestionCategoryPO questionCategoryPO = this.questionCategoryMapper.selectById(id);

        QuestionCategoryDO questionCategoryDO = BeanUtil.copyProperties(questionCategoryPO, QuestionCategoryDO.class);

        return questionCategoryDO;
    }


    @Override
    public List<QuestionCategoryDO> queryList(QuestionCategoryQuery request){

        Condition condition = request.toCondition();

        List<QuestionCategoryPO> poList = this.questionCategoryMapper.selectList(condition);

        List<QuestionCategoryDO> doList = BeanUtil.copyToList(poList, QuestionCategoryDO.class);

        return doList;
    }

    @Override
    public BasePager<QuestionCategoryDO> queryPager(QuestionCategoryQuery request){


        PageInfo<QuestionCategoryPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.questionCategoryMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, QuestionCategoryDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.questionCategoryMapper.deleteByIds(ids);
    }


}

